﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reflector.CodeModel;
using System.Collections;

/*
 * Accepts a DLL and identified the method declaration including
 * the search string. This search string can be a name of the class for
 * which the method sequence needs to be extracted
 */ 
namespace MethodSeqGenerator
{
    class AnalyzeDLLs
    {
        String searchString;
        List<String> dllArr;
        List<IMethodDeclaration> methodDeclArr ;

        List<IAssembly> loadedAssemblies;
        IAssemblyManager assemblyManager;

        public AnalyzeDLLs(IServiceProvider serviceProvider, String searchString, List<String> dllArr)
        {
            this.searchString = searchString;
            this.dllArr = dllArr;
            this.methodDeclArr = new List<IMethodDeclaration>();
            this.loadedAssemblies = new List<IAssembly>();

            //Loading all DLLs
            this.assemblyManager = (IAssemblyManager)serviceProvider.GetService(typeof(IAssemblyManager));

            //TODO: To later replace this method body with the actual implementation from code search
            foreach(String dllName in dllArr)
            {
                IAssembly rAssembly = assemblyManager.LoadFile(dllName);
                loadedAssemblies.Add(rAssembly);
                Console.WriteLine("Assembly: " + rAssembly.ToString());

                foreach (IModule module in rAssembly.Modules)
                {
                    foreach (ITypeDeclaration typeDeclaration in module.Types)
                    {
                        foreach (IMethodDeclaration methodDeclaration in typeDeclaration.Methods)
                        {
                            methodDeclArr.Add(methodDeclaration);
                        }
                    }
                }
                this.assemblyManager.Unload(rAssembly);
            }
        }

        ~AnalyzeDLLs()
        {
            /*foreach(IAssembly rAssembly in loadedAssemblies)
            {
                this.assemblyManager.Unload(rAssembly);
            }*/
        }

        //Getters and Setters
        public List<IMethodDeclaration> getMethodDeclArr()
        {
            return this.methodDeclArr;
        }
    }
}
